package com.woniuxy.oldobject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldobject.dto.NigthtTourDto;
import com.woniuxy.oldobject.entity.NightTour;
import com.woniuxy.oldobject.entity.ResponseEntity;
import com.woniuxy.oldobject.service.NightTourService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * description: NightTourController <br>
 * date: 2022/7/21 2:41 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("/nightTour")
public class NightTourController {
    @Autowired
    private NightTourService nightTourService;

    @PostMapping("/list")
    /**
     * 查询所有巡逻列表
     * 分页+带条件搜索
     */
    public ResponseEntity findAllNightToursPageAndConditions(@RequestBody NigthtTourDto nigthtTourDto) {
        PageHelper.startPage(nigthtTourDto.getPage(), nigthtTourDto.getLimit());
        List<NightTour> nightTourList = nightTourService.queryAll(nigthtTourDto);
        PageInfo<NightTour> nightTourPageInfo = new PageInfo<>(nightTourList);
        if ((nightTourList != null)) {
            return new ResponseEntity(200, "查询巡逻列表记录成功", nightTourPageInfo);
        } else {
            return ResponseEntity.ERROR;
        }
    }

    @PostMapping("/batch")
    public ResponseEntity saveBatchNightTour(@RequestBody NigthtTourDto nigthtTourDto) {
        int i = nightTourService.inserts(nigthtTourDto);
        if (i > 0) {
            return new ResponseEntity(200, "批量新增巡逻记录成功", i);
        } else {
            return ResponseEntity.ERROR;
        }
    }

    @PostMapping("/updateNightTour")
    public ResponseEntity UpdateNightTour(@RequestBody NightTour nightTour) {
        int i = nightTourService.update(nightTour);
        if (i > 0) {
            return new ResponseEntity(200, "修改巡逻记录成功", i);
        } else {
            return ResponseEntity.ERROR;
        }
    }

    @PostMapping("/delete/{id}")
    public ResponseEntity DeleteNightTour(@PathVariable("id") int id) {
        int i = nightTourService.deleteById(id);
        if (i > 0) {
            return new ResponseEntity(200, "删除巡逻记录成功", i);
        } else {
            return ResponseEntity.ERROR;
        }
    }
}
